package com.ls.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ls.controller.invite.model.AddInviteModel;
import com.ls.controller.invite.model.CloseTipModel;
import com.ls.controller.invite.model.EditInviteTypeModel;
import com.ls.entity.*;
import com.ls.mapper.InviteMapper;
import com.ls.service.*;
import com.ls.utils.ResultData;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import static com.ls.constant.SystemConstants.NUMBER_0;
import static com.ls.constant.SystemConstants.VALID_TRUE;

/**
 * <p>
 * 邀请函 服务实现类
 * </p>
 *
 * @author pengyu
 * @since 2024-10-12
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class InviteServiceImpl extends ServiceImpl<InviteMapper, Invite> implements IInviteService {
    private final IInviteFieldService inviteFieldService;
    private final ICompanyService companyService;
    private final IYxSmsService yxSmsService;
    private final IInviteTemplateService inviteTemplateService;

    /**
     * 新增邀请函
     */
    @Override
    public ResultData add(AddInviteModel model) {
        log.info("新增邀请函:{}", model);
        Invite invite = new Invite();
        BeanUtils.copyProperties(model, invite);
        if (model.getInviteId() == null) {
            YxSms yxSms=yxSmsService.getOne(new LambdaQueryWrapper<YxSms>().eq(YxSms::getYxSmsType,NUMBER_0)
                    .orderByAsc(YxSms::getCreateTime)
                    .last("LIMIT 1"));
            Company company = companyService.getOne(new LambdaQueryWrapper<Company>().eq(Company::getCustomerId, model.getCustomerId()));
            invite.setCompanyId(company.getCompanyId());
            invite.setCreateTime(LocalDateTime.now());
            invite.setUpdateTime(LocalDateTime.now());
            invite.setYxSmsId(yxSms.getYxSmsId());
            InviteTemplate inviteTemplate = inviteTemplateService.getOne(new LambdaQueryWrapper<InviteTemplate>()
                    .eq(InviteTemplate::getValidFlag, VALID_TRUE)
                            .orderByAsc(InviteTemplate::getCreateTime)
                    .last("LIMIT 1")
            );
            invite.setInviteTemplateId(inviteTemplate.getInviteTemplateId());
        } else {
            invite.setUpdateTime(LocalDateTime.now());
            inviteFieldService.remove(new LambdaQueryWrapper<InviteField>().eq(InviteField::getInviteId, model.getInviteId()));
        }
        saveOrUpdate(invite);
        log.info("新增邀请函成功:{}", invite.getInviteId());
        model.getFields().forEach(field -> {
            field.getList().forEach(child -> {
                InviteField inviteField = new InviteField();
                inviteField.setInviteId(invite.getInviteId());
                inviteField.setFieldType(field.getFieldType());
                inviteField.setFieldName(field.getFieldName());
                inviteField.setFieldIndex(child.getFieldIndex());
                inviteField.setFieldLabel(child.getFieldLabel());
                inviteField.setFieldValue(child.getFieldValue());
                inviteField.setFieldImg(child.getFieldImg());
                inviteField.insert();
            });
        });
        return ResultData.success();
    }
    /**修改邀请函选项*/
    @Override
    public ResultData edit(EditInviteTypeModel model) {
        log.info("修改邀请函选项:{}", model);
        Invite invite = getById(model.getInviteId());
        invite.setInviteTemplateId(model.getInviteTemplateId());
        invite.setYxSmsId(model.getYxSmsId());
        invite.setUpdateTime(LocalDateTime.now());
        saveOrUpdate(invite);
        return ResultData.success();
    }
    /**关闭提示*/
    @Override
    public ResultData closeTip(CloseTipModel model) {
        Invite invite = getById(model.getInviteId());
        invite.setIsShow(model.getIsShow());
        invite.setUpdateTime(LocalDateTime.now());
        saveOrUpdate(invite);
        return ResultData.success();
    }
    /**
     * 查询邀请函详情
     */
    @Override
    public ResultData getDetail(Long customerId) {
        Invite invite = getOne(new LambdaQueryWrapper<Invite>()
                .eq(Invite::getCustomerId, customerId)
                .orderByDesc(Invite::getCreateTime)
        );
        if (invite == null){
            return ResultData.successData(null);
        }else{
            Long inviteId = invite.getInviteId();
            AddInviteModel model = new AddInviteModel();
            BeanUtils.copyProperties(invite, model);
            List<AddInviteModel.info> infoList = new ArrayList<>();
            if (!invite.getMessage().isEmpty()){
                for (int i = 1; i <= 7; i++) {
                    AddInviteModel.info info = new AddInviteModel.info();
                    List<InviteField> list = inviteFieldService.list(new LambdaQueryWrapper<InviteField>()
                            .eq(InviteField::getInviteId, inviteId)
                            .eq(InviteField::getFieldType, i)
                    );
                    List<AddInviteModel.Child> childList = new ArrayList<>();
                    info.setFieldType(list.get(0).getFieldType());
                    info.setFieldName(list.get(0).getFieldName());
                    list.forEach(c -> {
                        AddInviteModel.Child child = new AddInviteModel.Child();
                        child.setFieldIndex(c.getFieldIndex());
                        child.setFieldLabel(c.getFieldLabel());
                        child.setFieldValue(c.getFieldValue());
                        child.setFieldImg(c.getFieldImg());
                        childList.add(child);
                    });
                    info.setList(childList);
                    infoList.add(info);
                }
            }
            model.setFields(infoList);

            return ResultData.successData(model);
        }
    }
}
